﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using Microsoft.Forums.MicrosoftForumsProvider;
using Microsoft.Forums.SyncService;
using Microsoft.Forums.OfflineStore;


namespace Microsoft.Forums.ForumsClient
{
    /// <summary>
    /// Class to represent a Microsoft brand
    /// </summary>
    public class Brand
    {
        private string brandName;

        /// <summary>
        /// Override default ToString so that it returns the brand name
        /// The default ToString returns the object type which doesn't work 
        /// with automated tests
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return BrandName;
        }

        /// <summary>
        /// Returns the brand name. Primary use is to provide a 
        /// Sort Description for the combo box for BrandCollection
        /// </summary>
        public string BrandName
        {
            get { return this.brandName; }
            set { this.brandName = value; }
        }
    }

    /// <summary>
    /// Collection of unsubscribed forums for currently selected brand
    /// </summary>
    public class BrandForumsCollection : ObservableCollection<Forum>
    {
        private static ObservableCollection<Forum> brandForumsCollection;

        /// <summary>
        /// Constructor just instantiates a new collection
        /// </summary>
        public BrandForumsCollection()
        {
            brandForumsCollection = new ObservableCollection<Forum>();
        }

        /// <summary>
        /// Returns the current collection
        /// </summary>
        public static ObservableCollection<Forum> AvailableForumsCollection
        {
            get { return brandForumsCollection; }
        }

        /// <summary>
        /// Method will add the forum object (parameter) to the collection if
        /// it is not in the Preview Area and is not currently in the database (i.e. it is not already subscribed)
        /// </summary>
        /// <param name="forum"></param>
        /// <param name="databaseConnectionString">name of database file</param>
        public static void AddToBrandForumsCollection(Forum forum, string connectionString)
        {
            if (forum == null)
                throw new ArgumentNullException("forum","forum cannot be null");

            ForumsOfflineStore store = new ForumsOfflineStore(connectionString);

            if ((IsForumInPrevewArea(forum) == false) && (store.Forums.Any(f => f.Id == forum.Id) == false))
            {
                brandForumsCollection.Add(forum);
            }
        }

        /// <summary>
        /// Returns true if the forum object (parameter) is in the preview area
        /// </summary>
        /// <param name="forum"></param>
        /// <returns></returns>
        private static bool IsForumInPrevewArea(Forum forum)
        {
            bool forumIsInPreviewArea = false;
            foreach (Forum previewForm in PreviewForumsCollection.Instance)
            {
                if (forum.Id == previewForm.Id)
                {
                    forumIsInPreviewArea = true;
                    break;
                }
            }
            return forumIsInPreviewArea;
        }
    }

    /// <summary>
    /// Collection of brands returned by GetAvailableForums API
    /// </summary>
    public class BrandsCollection : ObservableCollection<Brand>
    {
        private ObservableCollection<Brand> brandCollection;

        /// <summary>
        /// Constructor just instantiates a new ObservableCollection of Brand objects
        /// </summary>
        public BrandsCollection()
        {
            brandCollection = new ObservableCollection<Brand>();
        }

        /// <summary>
        /// Returns ObservableCollection of Brand objects
        /// </summary>
        public ObservableCollection<Brand> BrandCollection
        {
            get { return brandCollection; }
        }
    }
}
